package com.zxc.controller.customer;

import com.zxc.entity.CstCustomer;
import com.zxc.entity.SysUser;
import com.zxc.service.CstCustomerService;
import com.zxc.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "/customer")
public class CustomerController {

    @Autowired
    private CstCustomerService cstCustomerService;

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping({"/{custNo}","/getCustomerByCustNo/{custNo}"})
    @ResponseBody
    public Map<String,Object> getCstCustomerById(@PathVariable Integer custNo){
        Map<String,Object> modelMap = new HashMap<>();
        CstCustomer cstCustomer = cstCustomerService.selectCstCustomerById(custNo);
        if (cstCustomer != null){
            modelMap.put("success",true);
            modelMap.put("customer",cstCustomer);
        }
        return modelMap;
    }

    @RequestMapping({"/{skipCount}/{pageSize}/{searchName}","/{skipCount}/{pageSize}"})
    @ResponseBody
    public Map<String,Object> getCustomerListOrByCustName(@PathVariable Integer skipCount,
                                  @PathVariable Integer pageSize,
                                  @PathVariable(required = false) String searchName){
        Map<String,Object> modelMap = new HashMap<>();
        //计算出略过的记录数
        int skipCounts = (skipCount-1)*pageSize;
        List<CstCustomer> cstCustomerList = cstCustomerService.getCstCustomerList(skipCounts,pageSize,searchName);
        modelMap.put("dataList",cstCustomerList);
        //这里需要的是没有客户经理的客户的总数，不是当前分页查询出来的人数的总数
        //modelMap.put("total",cstCustomerList.size());
        int total = cstCustomerService.selectCountCustomerForCustomer();
        modelMap.put("total",total);
        return modelMap;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object > save(CstCustomer cstCustomer){
        Map<String,Object> modelMap = new HashMap<>();

        //根据传入的创建人的信息来查询得到创建人的姓名
        List<SysUser> sysUserList = sysUserService.selectSysUserById(Integer.parseInt(cstCustomer.getCustCreater()));
        SysUser sysUser =  sysUserList.get(0);
        //给客户赋值默认值
        cstCustomer.setCustCreater(sysUser.getUserName());
        cstCustomer.setCustSatisfy(1);
        cstCustomer.setCustManagerName("0");
        cstCustomer.setCustStatus("1");
        cstCustomer.setCreateTime(new Date());
        int result = cstCustomerService.saveCstCustomer(cstCustomer);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }

    @RequestMapping(value = "/getSalChanceByChcId/{chcId}")
    @ResponseBody
    public Map<String,Object> getSalChanceByChcId(@PathVariable(value = "chcId") Integer chcId){
        Map<String,Object> modelMap = new HashMap<>();
        CstCustomer cstCustomer = cstCustomerService.selectCstCustomerById(chcId);
        modelMap.put("customer",cstCustomer);
        if (cstCustomer == null){
            modelMap.put("success",false);
        }else {
            modelMap.put("success",true);
        }
        return modelMap;
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public Map<String,Object> updateCustomerByCustNo(CstCustomer cstCustomer, String commonCreater){
        Map<String,Object> modelMap = new HashMap<>();
        List<SysUser> sysUserList = new ArrayList<>();
        if (!"".equals(cstCustomer.getCustCreater())){
            sysUserList = sysUserService.selectSysUserById(Integer.parseInt(cstCustomer.getCustCreater()));
        }else {
            System.out.println("修改时没有填写创建人");
            System.out.println("需要查询的创建人的名字："+commonCreater);
            sysUserList = sysUserService.selectSysUserBySysUserName(commonCreater);
        }
        SysUser sysUser = sysUserList.get(0);
        cstCustomer.setCustCreater(sysUser.getUserName());
        System.out.println("客户模块修改客户信息时更新时客户的信息"+commonCreater);
        int result = cstCustomerService.updateCustomerByCustNo(cstCustomer);
        if (result>0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> delete(Integer[] custNo){
        Map<String,Object> modelMap = new HashMap<>();
        int result = cstCustomerService.delete(custNo);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }
    @RequestMapping(value = "/getCustomerName")
    @ResponseBody
    public List<String> getCustName(String keyword){
        System.out.println("自动补全按钮生效了");
        List<String> stringList = cstCustomerService.getCustomerByCustName(keyword);
        return stringList;
    }

}
